<template>
  <el-container id="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="header">
        <img src="~assets/heima.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse? '64px': '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单 -->
        <el-menu
          :router='true'
          :collapse="isCollapse"
          :collapse-transition= 'false'
          unique-opened
          background-color="#3e4447"
          text-color="#fff"
          active-text-color="#5596de"
          :default-active="$route.path"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="index + ''"
            v-for="(item, index) in meunList"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.fontclass"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' +   item2.path"
              v-for="item2 in item.children"
              :key="item2.id"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <span>{{ item2.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧主题 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "Home",
  components: {},
  created() {
    this.getMeunList();
  },
  data() {
    return {
      meunList: [],
      fontclass: [
        "iconfont icon-users",
        "iconfont icon-tijikongjian",
        "iconfont icon-shangpin",
        "iconfont icon-danju",
        "iconfont icon-baobiao",
      ],
      isCollapse: false
    };
  },
  methods: {
    logout() {
      window.sessionStorage.removeItem("token");
      this.$router.push("/login");
    },
    async getMeunList() {
      const { data: res } = await this.$request("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.meunList = res.data.map((item, index) => {
        item.fontclass = this.fontclass[index];
        return item;
      });
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  },
};
</script>

<style lang="less" scoped>
#home-container {
  height: 100vh;
  .el-header {
    display: flex;
    justify-content: space-between;
    color: #fff;
    background-color: #3e4447;
    padding-left: 0;
    align-items: center;
    font-size: 20px;
    .header {
      display: flex;
      justify-content: center;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    .toggle-button {
      display: flex;
      justify-content: center;
      background-color: #4a5064;
      line-height: 24px;
      color: #fff;
      font-size: 10px;
      letter-spacing: 2px;
    }
    background-color: #3a3f4a;
    .el-menu {
      border-right: 0;
    }
  }
  .el-main {
    background-color: #d8dcdf;
  }
}
.iconfont {
  margin-right: 10px;
}
</style>
